package com.app.megaprosoft.modyimcarcare;

import java.io.File;
import java.sql.ResultSet;

import com.app.megaprosoft.DB.SQLiteDBClass;
import com.megaprosoft.modyimcarcare.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Application;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewStub;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;


public class CopyOfCarcare_Login extends Activity{
	
	public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
	private ProgressDialog mProgressDialog;
	private Animation shake;
	private AlertDialog loginDialog;
	private AlertDialog showloginDialog;
	private int i=1;
	
	View viewstub_company,viewstub_user;
	ViewStub stub_company,stub_user;
	private RadioButton[] radioButton = new RadioButton[10];
	LinearLayout linear_company;
	
	
	ResultSet rs;
	private Boolean ck_login = false;
	
	private String   FFUserOID
					,UserName
					,Password
					,DepCode
					,DepName
					,UserGroupCode
					,UserGroupName
					,PicPath
					,CompanyName
					,CompanyOID;
	
	
	
	EditText ED_Password,ED_UserName;
	Spinner SP_Company;
	View imagebutton_bluetooth;
	View imagebutton_camera;
	SQLiteDBClass myDb;
	Intent Main;
	String arrData[];
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.a_carcare_activity_loginbg);
		
		shake = AnimationUtils.loadAnimation(this, R.anim.shake);
		myDb = new SQLiteDBClass(this);
		
		Main = new Intent(CopyOfCarcare_Login.this,Carcare_Main.class);
		
		LayoutInflater inflater = CopyOfCarcare_Login.this.getLayoutInflater();
		
        loginDialog = new AlertDialog.Builder(CopyOfCarcare_Login.this)
        .setView(inflater.inflate(R.layout.a_carcare_activity_login, null))
        .setPositiveButton("เข้าสู่ระบบ", new DialogInterface.OnClickListener(){
        	@Override
        	public void onClick(DialogInterface dialog,int which){
        		
        	}
        }).setNegativeButton("ออกโปรแกรม", new DialogInterface.OnClickListener(){
        	@Override
        	public void onClick(DialogInterface dialog,int which){
        		
        	}
        }).create();
        
        loginDialog.setOnShowListener(new DialogInterface.OnShowListener() {
			
			@Override
			public void onShow(DialogInterface dialog) {
//				stub_company = (ViewStub) loginDialog.findViewById(R.id.viewStub_company);
				stub_company.setLayoutResource(R.layout.a_carcare_layout_company);
				viewstub_company = stub_company.inflate();
//				stub_user = (ViewStub) loginDialog.findViewById(R.id.viewStub_company);
//				stub_user.setLayoutResource(R.layout.a_carcare_layout_company);
//				viewstub_user = stub_user.inflate();
				
				
				
				
//				radioButton1 = (RadioButton) viewstub_company.findViewById(R.id.Carcare_radioButton1);
//				radioButton2 = (RadioButton) viewstub_company.findViewById(R.id.Carcare_radioButton2);
				
//				ED_Password = (EditText) loginDialog.findViewById(R.id.a_carcare_password);
//				ED_UserName = (EditText) loginDialog.findViewById(R.id.a_carcare_username);
//				SP_Company = (Spinner) loginDialog.findViewById(R.id.a_carcare_Company);
//				final InputMethodManager imm = (InputMethodManager)getSystemService(
//					      Context.INPUT_METHOD_SERVICE);
//				imm.hideSoftInputFromWindow(ED_Password.getWindowToken(), 0);
//				imm.hideSoftInputFromWindow(ED_UserName.getWindowToken(), 0);
				
//				Define_Company();
				Define_Company();
				// TODO Auto-generated method stub
				Button button_OK = loginDialog.getButton(AlertDialog.BUTTON_POSITIVE);
				Button button_Cancle = loginDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
				
				button_OK.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						radioButton[1].setChecked(true);
						// TODO Auto-generated method stub\
//						imm.hideSoftInputFromWindow(ED_Password.getWindowToken(), 0);
//						getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
//						new  LoadingLogin().execute();
					}
					
				});
				button_Cancle.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
//		                onBackPressed();
						radioButton[1].setChecked(false);
					}
					
				});
				
				
			}
		});
        
        loginDialog.show();
	}
	
	public void Define_Company(){
		linear_company = (LinearLayout) viewstub_company.findViewById(R.id.ewrewrewrewr);
		linear_company.setOrientation(LinearLayout.VERTICAL); 
		linear_company.removeAllViews();
//		linear_company.
		for(i=1;i<4;i++){
			radioButton[i] = new RadioButton(viewstub_company.getContext());
			radioButton[i].setText("มดขยันสาขา "+i);
//				a.setId(i);
//				radioButton[i].setWidth(200);
//				radioButton[i].setPadding(0, 10, 0, 0);
			radioButton[i].setTextSize(28);
				linear_company.addView(radioButton[i]);
//				radioButton[i] = (RadioButton) viewstub_company.findViewById(i);
				
				radioButton[i].setOnCheckedChangeListener( new OnCheckedChangeListener() {
					@Override
					   public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
					    // TODO Auto-generated method stub
//						Define_Company();
						for(int i = 1; i < 4; i++) { 
//							radioButton[i].setChecked(false);
							if(radioButton[i].getText().toString().equals(arg0.getText().toString())){
								radioButton[i].setChecked(true);
							}
//							
			            }  
						
					    Toast.makeText(viewstub_company.getContext(), "Check box "+arg0.getText().toString()+" is "+String.valueOf(arg1), Toast.LENGTH_SHORT).show();
					   }
				} );
		}
	}
	
	@SuppressWarnings("deprecation")
	public void Define_Company2(){
//		SP_Company = (Spinner) loginDialog.findViewById(R.id.a_carcare_Company);
//		final Cursor myData = myDb.SelectAll_Company();   
//		startManagingCursor(myData);
//		 // spinner1
//        final Spinner spin = (Spinner)loginDialog.findViewById(R.id.a_carcare_Company);  
//        String[] cols = new String[]{"CompanyName"};
//        int[] names = new int[]{R.id.textView1};
//        
//        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list_spin_credit , myData ,cols,names);
//       	
//        spin.setAdapter(adapter); 
//        
//        spin.setOnItemSelectedListener(new OnItemSelectedListener() {
//
//			public void onItemSelected(AdapterView<?> arg0, View selectedItemView,
//					int position, long id) {
//				// TODO Auto-generated method stub
//				CompanyName = myData.getString(myData.getColumnIndex("CompanyName"));
//				CompanyOID = myData.getString(myData.getColumnIndex("CompanyOID"));
//				
////        	    Toast.makeText(Carcare_Login.this,
////        	    		"Your Selected : " + ItemID,
////        	    			Toast.LENGTH_SHORT).show();	
//			}
//
//			public void onNothingSelected(AdapterView<?> arg0) {
//				// TODO Auto-generated method stub
////	      	    Toast.makeText(Carcare_Login.this,
////        	    		"Your Selected : -",
////        	    			Toast.LENGTH_SHORT).show();	
//			}
//
//
//        });
   
	}

	 @Override
	    protected Dialog onCreateDialog(int id) {
	        switch (id) {
	        case DIALOG_DOWNLOAD_PROGRESS:
	            mProgressDialog = new ProgressDialog(this);
	            mProgressDialog.setMessage("Checking.....");
	            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
	            mProgressDialog.setCancelable(true);
	            mProgressDialog.show();
	            return mProgressDialog;
	        default:
	            return null;
	        }
	    }
	 	//-----------AsyncTask-Loading----------------------------------//
//	    public class LoadingLogin extends AsyncTask<String, Void, Void> {
//	        @SuppressWarnings("deprecation")
//			protected void onPreExecute() {
//	        	super.onPreExecute();
////	        	ED_Password = (EditText) loginDialog.findViewById(R.id.a_carcare_password);
////	        	ED_UserName = (EditText) loginDialog.findViewById(R.id.a_carcare_username);
////	        	SP_Company = (Spinner) loginDialog.findViewById(R.id.a_carcare_Company);
//	        	showDialog(DIALOG_DOWNLOAD_PROGRESS);
//	        }
//	        
//			@Override
//	        protected Void doInBackground(String... params) {
//				
//	        	try{
//	        		UserName=ED_UserName.getText().toString();
//	        		if(!UserName.equals("")){
//	        			arrData = myDb.Select_FFUser(UserName);
//	        			if(arrData != null ){
//		        			Password=ED_Password.getText().toString();
//		        			if(arrData[3].equals(Password)){
//	        					ck_login=true;
//	        				}else{
//	        					ED_Password.startAnimation(shake);
//	        				}
//		        		}else{
//		        			ED_UserName.startAnimation(shake);
//		        		}
//		        		FFUserOID 		= arrData[0];
//		        		UserName		= arrData[2];
//		        		DepCode 		= arrData[4];
//		        		DepName 		= arrData[5];
//		        		UserGroupCode 	= arrData[6];
//		        		UserGroupName 	= arrData[7];
//		        		PicPath 		= arrData[8];
//	        		}else{
//	        			ED_UserName.startAnimation(shake);
//	        		}
//	        	}catch(Exception e){
//	        		
//	        	}
//	        	SystemClock.sleep(1000);
//	    		return null;
//	        }
//
//	        @SuppressWarnings("deprecation")
//			protected void onPostExecute(Void unused) {
//	        	dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
//		        removeDialog(DIALOG_DOWNLOAD_PROGRESS);
//	        	if(ck_login==true){
//	 	           Main.putExtra("CompanyOID", CompanyOID);
//	 	           Main.putExtra("CompanyName", CompanyName);
//	 	           Main.putExtra("FFUserOID", FFUserOID);
//	 	           Main.putExtra("UserName", UserName);
//	 	           Main.putExtra("DepCode", DepCode);
//	 	           Main.putExtra("DepName", DepName);
//	 	           Main.putExtra("UserGroupCode", UserGroupCode);
//	 	           Main.putExtra("UserGroupName", UserGroupName);
//	 	           Main.putExtra("PicPath", PicPath);
//	 			   startActivity(Main);
//	 		       finish();
//	 		       
//	        	}else{
//	        		ED_Password.setText("");
//	        	}
//	           
//	        } 
//	    }
//	    
	 
	 @Override
		public void onBackPressed() {
		  
		        new AlertDialog.Builder(this)
		        .setTitle("ออกจากโปรแกรม")
		        .setMessage("ต้องการออกจากโปรแกรม")
		                .setNegativeButton(android.R.string.yes, new DialogInterface.OnClickListener() {

		                    public void onClick(DialogInterface arg0, int arg1) {
		                        finish();
		                        System.exit(0);
		                    }
		                })
		                .setPositiveButton(android.R.string.no, new DialogInterface.OnClickListener() {

		                    public void onClick(DialogInterface arg0, int arg1) {
		                    	loginDialog.show();
		                    }
		                }).create().show();
		} 

	
}
